package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.security.SecureRandom;
import java.util.Date;

//配置bean  web层的配置
    //@Controller
      @RestController  //@RestController = @Controller + @ResponseBody
      @RequestMapping("/rest")
    public class UserController {


        //设置映射路径为/save，即外部访问路径
        @RequestMapping("/save")
        //设置当前操作返回结果为字符串
        @ResponseBody
        public String save() {
            System.out.println("user save ...");
            return "hello";
        }

        //普通参数
        @RequestMapping("/commonParam")
        @ResponseBody
        public String commonParam(String name,Integer age){
            System.out.println(name+","+age);
            return "{hello}";
        }

        //pojo参数
        @RequestMapping("/pojoParam")
        @ResponseBody
        public String pojoParam(User user){
            System.out.println("pojo参数传递 user ==> "+user);
            return "hello";
        }

        //接收json数据
        @RequestMapping("/json")
        @ResponseBody
        public String pojoParamForJson(@RequestBody User user){
            System.out.println("pojo(json)参数传递 user ==> "+user);
            return "{'module':'pojo for json param'}";
        }

        //传递参数命不一致

    @RequestMapping("/differentParam")
    @ResponseBody
    public String param( @RequestParam("name")String username, Integer age){
        System.out.println(username+","+age);
        return "{中文}";
    }

    //日期参数
    //使用@DateTimeFormat注解设置日期类型数据格式，默认格式yyyy/MM/dd
    @RequestMapping("/data")
    @ResponseBody
    public String dataParam(Date date){
        System.out.println("参数传递 date ==> "+date);
        //System.out.println("参数传递 date1(yyyy-MM-dd) ==> "+date1);
        //System.out.println("参数传递 date2(yyyy/MM/dd HH:mm:ss) ==> "+date2);
        return "{'module':'data param'}";
    }

    @RequestMapping(value = "/method32",produces = "text/html;charset=utf-8")
    @ResponseBody // 返回值将被设置到响应体中
    public Object method02() {
        return "hello 中文2";
    }



   // @RequestMapping(value = "/rest",method = RequestMethod.GET)
   // @ResponseBody
    @GetMapping
    public String demo01(){
        System.out.println("findAll");
        return "findAll";
    }

    //@RequestMapping(value = "/rest/{id}",method = RequestMethod.GET)
    //@ResponseBody
    @GetMapping("/{id}")
    public String demo11(@PathVariable Integer id){
        System.out.println("findById : " + id);
        return "findById";
    }

   // @RequestMapping(value = "/rest/{page}/{pageSize}",method = RequestMethod.GET)
    //@ResponseBody
    @GetMapping("/{page}/{pageSize}")
    public String demo12(@PathVariable Integer page,@PathVariable Integer pageSize){
        System.out.println("findByPage : " + page + "," + pageSize);
        return "findByPage";
    }

    //@RequestMapping(value = "/rest",method = RequestMethod.DELETE)
    //@ResponseBody
    @DeleteMapping
    public String demo02(){
        System.out.println("deleteAll");
        return "deleteAll";
    }
    //@RequestMapping(value = "/rest/{id}",method = RequestMethod.DELETE)
    //@ResponseBody
    @DeleteMapping("/{id}")
    public String demo21(@PathVariable Integer id){
        System.out.println("deleteById : " + id);
        return "deleteById";
    }

    //@RequestMapping(value = "/rest",method = RequestMethod.POST)
    //@ResponseBody
    @PostMapping()
    public String demo03(@RequestBody User user){
        System.out.println("add: " + user);
        return "add";
    }


    //@RequestMapping(value = "/rest",method = RequestMethod.PUT)
    //@ResponseBody
    @PutMapping
    public String demo04(){
        System.out.println("update");
        return "update";
    }

    }


